#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
vector<set<int> > V;
//struct Node{
//    set<int> adj;
//    bool operator < (const Node& r)const {
//        return d > r.d;
//    }
//}V[maxn];

bool cmp(const int &i, const  int& j){
    return V[i].d > V[j].d;
}

int num;
int id[maxn];
int ID(int i) {
    if(id[i]) return id[i];
    else id[num++] = i;
}
//map<int, int> id;
//set<int, cmp> sn;


//int ID(int i){
//    if(!id.count(i)){
//        id[i] = num++;
//    }
//    return id[i];
//}

void init(int i, int j){

}
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int T; cin >> T;
    for (int kase = 1; kase <= T; ++kase) {
//        memset(V, 0, sizeof(V));
        int N; cin >> N;
        set<int> s;
        num = 0;
        for (int i = 0; i < N; ++i) {
            int a, b; cin >> a >> b;
            int ia = ID(a), ib = ID(b);
            if(!V[ia].adj.count(b)){
                V[ia].adj.insert(b);

            }
            V[ID(a)].adj.insert(b);
            V[ID(a)].d++;
            V[ID(b)].adj.insert(a);
            V[ID(b)].d++;

        }

    }
    return 0;
}